package cn.fl.flow.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.flow.api.ISysFlowRejRsnApiService;
import cmm.comm.facade.flow.condition.SysFlowRejRsnQueryCondition;
import cmm.comm.facade.flow.dto.SysFlowRejRsnDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.flow.service.ISysFlowRejRsnService;
import cn.fl.flow.transform.SysFlowRejRsnVOTransform;
import cn.fl.flow.vo.SysFlowRejRsnVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Description: 流程驳回原因表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class SysFlowRejRsnServiceImpl implements ISysFlowRejRsnService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(ISysFlowRejRsnService.class);
	
	/**
	 * 流程驳回原因API服务
	 */
	@Autowired
	private ISysFlowRejRsnApiService sysFlowRejRsnApiService;

	@Override
	public SysFlowRejRsnVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		SysFlowRejRsnDTO dto = this.sysFlowRejRsnApiService.selectById(id);//this.自定义ApiService.selectById(id);
		return SysFlowRejRsnVOTransform.toVO(dto);
	}

	@Override
	public Long add(SysFlowRejRsnVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.sysFlowRejRsnApiService.add(SysFlowRejRsnVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id,String rsnTypCd,String orgCd) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		List<SysFlowRejRsnDTO> sysFlowRejRsnList = this.sysFlowRejRsnApiService.selectRejRsnAllChild(id,rsnTypCd,orgCd,CurrentThreadContext.getCurrentSysCd());
		boolean removeResult =false;
		if(CollectionUtils.isNotEmpty(sysFlowRejRsnList)){
			for (SysFlowRejRsnDTO sysFlowRejRsnDTO : sysFlowRejRsnList) {
				removeResult = this.sysFlowRejRsnApiService.removeById(sysFlowRejRsnDTO.getId());
				if(!removeResult){
					removeResult =false;
				}
			}
		}
		return  removeResult;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(SysFlowRejRsnVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.sysFlowRejRsnApiService.modify(SysFlowRejRsnVOTransform.toDTO(entity));
		//TODO 业务逻辑
		//return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		//return this.sysFlowRejRsnApiService.searchListPage(page);
		//TODO 业务逻辑
		return null;
	}

	@Override
	public List<SysFlowRejRsnVO> seachSysFlowRejRsnList(Long pid, String rsnTypCd) throws BaseException {
		if(StringUtils.isEmpty(rsnTypCd)||StringUtils.isEmpty(pid)){
			BaseLogUtils.info(logger, "seachSysFlowRejRsnList", "根据父ID和驳回意见类型查询驳回意见集合失败,参数不完整.DATA:[pid:"+pid+",rsnTypCd:"+rsnTypCd+"]", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		List<SysFlowRejRsnDTO> DTOList = this.sysFlowRejRsnApiService.searchSysFlowRejRsnList(pid, rsnTypCd,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		return SysFlowRejRsnVOTransform.toVOList(DTOList);
	}

	@Override
	public List<SysFlowRejRsnVO> searchList(SysFlowRejRsnQueryCondition qc)
			throws BaseException {
		if(null==qc)qc = new SysFlowRejRsnQueryCondition();
		List<SysFlowRejRsnDTO> DTOList = this.sysFlowRejRsnApiService.searchList(qc);
		return SysFlowRejRsnVOTransform.toVOList(DTOList);
	}

	@Override
	public JSONArray searchRejRsnTreeByCon(Long id,String rsnTypCd, String buOrgCd,String sysCd)
			throws BaseException {
		List<SysFlowRejRsnDTO> orgList = this.sysFlowRejRsnApiService.searchRejRsnTreeByCon(id,rsnTypCd,buOrgCd, sysCd);
		JSONArray jsonArray = new JSONArray ();
		if(CollectionUtils.isEmpty(orgList)){
			return jsonArray;
		}
		for (SysFlowRejRsnDTO sysFlowRejRsnDTO : orgList) {
			JSONObject obj = new JSONObject();
			obj.put("ischecked", false);
			obj.put("id", sysFlowRejRsnDTO.getId());
			obj.put("text", sysFlowRejRsnDTO.getRsnDesc());
			obj.put("pid", sysFlowRejRsnDTO.getPid());
			obj.put("orgCd", sysFlowRejRsnDTO.getOrgCd());
			obj.put("rsnTypCd", sysFlowRejRsnDTO.getRsnTypCd());
			
			List<SysFlowRejRsnDTO> sunList = this.sysFlowRejRsnApiService.searchRejRsnTreeByCon(sysFlowRejRsnDTO.getId(),rsnTypCd,buOrgCd, sysCd);
			if(CollectionUtils.isNotEmpty(sunList)){
				obj.put("hasChildren", true);
			}
			
			jsonArray.add(obj);
		}
		//JSONArray treeList = this.treePerList(jsonArray,SysFlowRejRsnConstant.SYS_FLOW_REJ_RSN_PID);
		return jsonArray;
	}
	
	/**
	 * 递归添加整个集合数据
	 * @param treeList
	 * @param parentId
	 * @return
	 */
	private JSONArray treeList(JSONArray treeList, Long parentId) {  
	       JSONArray childMenu = new JSONArray();  
	       for (Object object : treeList) {  
	           JSONObject jsonMenu = JSONObject.fromObject(object);  
	           long oid = jsonMenu.getLong("id"); 
	           long pid = jsonMenu.getLong("pid"); 
	           if (parentId == pid) {  
	               JSONArray c_node = treeList(treeList, oid);  
	               jsonMenu.put("children", c_node);  
	               childMenu.add(jsonMenu);  
	           }  
	       }  
	       return childMenu;  
	   }
}
